package com.barunsw.bwf.ems.neoarena.node;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.barunsw.bwf.ems.common.constants.Result;
import com.barunsw.bwf.ems.common.controller.CommonController;
import com.barunsw.bwf.ems.common.dto.ResultVo;
import com.barunsw.bwf.ems.dto.NodeVo;
import com.barunsw.bwf.ems.util.IPUtil;

@Controller
@RequestMapping("/node")
public class NodeController extends CommonController {
	private static final Logger LOGGER = LoggerFactory.getLogger(NodeController.class);
	
	@Autowired
	NodeService nodeService;
	
	/**
	 * Node 리스트 조회
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	public @ResponseBody List<NodeVo> getNodeList() throws Exception {
		return nodeService.getNodeList();
	}

	/**
	 * Node 정보 조회
	 * 
	 * @param nodeId 조회하고자 하는 Node
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/{nodeId}", method = RequestMethod.GET)
	public @ResponseBody NodeVo getNode(@RequestBody @PathVariable long nodeId) throws Exception {
		return nodeService.getNode(nodeId);
	}
	
	/**
	 * Node 정보 추가
	 * 
	 * @param node
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "", method = RequestMethod.POST)
	public @ResponseBody ResultVo createNode(@RequestBody NodeVo nodeVo) throws Exception {
		LOGGER.debug(String.format("createNode node_id:%s, tid:%s, ip:%s", nodeVo.getNode_id(), nodeVo.getTid(), nodeVo.getIp()));
		ResultVo resultVo = new ResultVo(Result.OK, "");
		
		if ( nodeVo.getIp() == null ) {
			resultVo.setResult(Result.NOK);
			resultVo.setReason("IP가 존재하지 않습니다.");
			
			return resultVo;
		}
		
		long nodeId = IPUtil.convertLong(nodeVo.getIp());
		nodeVo.setNode_id(nodeId);
		
		NodeVo existNode = nodeService.getNode(nodeId);
		if ( existNode != null ) {
			resultVo.setResult(Result.NOK);
			resultVo.setReason("해당 IP에 노드가 이미 존재합니다.");
			
			return resultVo;
		}

		int result = nodeService.createNode(nodeVo);

		if ( result == 0 ) {
			resultVo.setResult(Result.NOK);
			resultVo.setReason("DB 추가시 오류가 발생하였습니다.");
		}
		
		return resultVo;
	}
	
	/**
	 * Node 정보 변경
	 * 
	 * @param node
	 * @param nodeId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "", method = RequestMethod.PUT)
	public @ResponseBody ResultVo changeNode(@RequestBody NodeVo nodeVo) throws Exception {
		ResultVo resultVo = new ResultVo(Result.OK, "");
		
		int result = nodeService.changeNode(nodeVo);
		if ( result == 0 ) {
			resultVo.setResult(Result.NOK);
			resultVo.setReason("노드 정보 변경 중 오류가 발생하였습니다.");
		}

		return resultVo;
	}

	/**
	 * Node 정보 삭제
	 * 
	 * @param nodeId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/{nodeId}", method = RequestMethod.DELETE)
	public @ResponseBody ResultVo deleteNode(@PathVariable long nodeId) throws Exception {
		ResultVo resultVo = new ResultVo(Result.OK, "");
		
		int result = nodeService.deleteNode(nodeId);
		
		if ( result == 0 ) {
			resultVo.setResult(Result.NOK);
			resultVo.setReason(String.format("해당 노드(node_id:%s)가 존재하지 않습니다.", nodeId));
		}
		
		return resultVo;
	}
}